<template>
	<!-- 配送单 -->
	<view class="page">
		<view class="order-nav">
			<view v-for="(statusItem, statusIndex) in statusList" :key="statusIndex" class="uni-tab-item" :id="statusItem.id"
			 :data-current="statusIndex" @click="ontabtap">
				<text class="uni-tab-item-title" :class="statusItem.status === orderStatus ? 'uni-tab-item-title-active color-base-border  color-base-text' : ''">
					{{ statusItem.name }}
				</text>
			</view>
		</view>
		<mescroll-uni @getData="getData" ref="mescroll" top="100">
			<block slot="list">
				<view class="order-group" v-if="dataList.length!=0">
					<view class="order-item" v-for="(item,index) in dataList" :key="index">
						<view class="order-title">
							<text>{{item.delivery_no}}</text>
							<text class="color-red">{{item.delivery_status_info.name}}</text>
						</view>
						<view class="msg-box">
							<view class="msg-spell">
								<text class="msg-title">团长信息</text>
								<text class="msg-name">{{item.leader_name}}</text>
							</view>
							<view class="msg-phone" @click="tack_phone(item)">
								<image :src="$util.img('upload/uniapp/community/phone.png')" mode="widthFix"></image>
								{{item.leader_mobile}}
							</view>
						</view>
						<view class="msg-box">
							<view class="msg-spell">
								<text class="msg-title">配送人员</text>
								<text class="msg-name">{{item.clerk_name}}</text>
							</view>
							<view class="msg-phone">
								<image :src="$util.img('upload/uniapp/community/phone.png')" mode="widthFix"></image>
								{{item.clerk_mobile}}
							</view>
						</view>
						<view class="msg-address">
							<text class="address-title">配送地址</text>
							<text class="address-text">{{item.address}}{{item.leader_community}}</text>
						</view>
						<view class="footer-box">
							<text>共{{item.goods_num}}件商品</text>
							<view class="btn-group">
								<button type="default" @click="$util.redirectTo('/otherpages/community/community_detail/delivery_detail?id='+item.delivery_id)">配送详情</button>
								<button type="default" @click="confirm(item)" v-if="item.delivery_status_info.leader_action.length!=0">确认收货</button>
							</view>
						</view>
					</view>
				</view>
				<view class="none_data" v-if="dataList.length==0">
					<image :src="$util.img('/upload/uniapp/common-empty.png')" mode="widthFix"></image>
					<text>暂时没有更多的配送信息了</text>
				</view>
			</block>
		</mescroll-uni>
		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusList: [],
				orderStatus: '',
				dataList: [],
				token: ''
			}
		},
		onLoad() {
			this.getBanner()
			if (!uni.getStorageSync('token')) {
				this.$refs.login.open('/pages/member/index/index');
			} else {
				this.token = uni.getStorageSync('token')
			}
			uni.setNavigationBarTitle({
				title: '我的配送单'
			});
		},
		methods: {
			tack_phone(e){
				uni.makePhoneCall({
				    phoneNumber: e.leader_mobile
				});
			},
			ontabtap(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				this.orderStatus = this.statusList[index].status;
				this.$refs.loadingCover.show();
				this.$refs.mescroll.refresh();
			},

			getBanner() {
				this.$api.sendRequest({
					url: '/api/communitydelivery/orderStatus',
					data: {
						// site_id: this.$config.siteId,
					},
					success: res => {
						this.statusList.push({
							status: '',
							name: '全部',
							id: 0
						})
						let dataGroup = res.data
						for (let item in dataGroup) {
							this.statusList.push({
								status: dataGroup[item].status,
								name: dataGroup[item].name,
								id: parseInt(item) + 1
							})
						}
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					},
					fail: res => {
						mescroll.endErr();
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			confirm(e) {
				let delivery_id = e.delivery_id
				uni.showModal({
					title: '温馨提示',
					content: '确定收到配送货物了吗？',
					success: (res) => {
						if (res.confirm) {
							this.confirm2(delivery_id)
						}
					}
				});
			},
			confirm2(e) {
				this.$api.sendRequest({
					url: '/api/communitydelivery/orderComplete',
					data: {
						token: this.token,
						// site_id: this.$config.siteId,
						delivery_ids: e
					},
					success: res => {
						uni.showToast({
							title: '已确认收货',
							icon: 'none'
						})
						setTimeout(() => {
							this.$refs.mescroll.refresh();
						}, 500)
					},
					fail: res => {
						mescroll.endErr();
					}
				});
			},
			getData(mescroll) {
				this.mescroll = mescroll
				this.$api.sendRequest({
					url: '/api/communitydelivery/orderList',
					data: {
						page: mescroll.num,
						page_size: mescroll.size,
						// site_id: this.$config.siteId,
						token: this.token,
						role_type: 'leader',
						delivery_status: this.orderStatus
					},
					success: res => {
						let newArr = [];
						let msg = res.message;
						if (res.code == 0 && res.data) {
							res.data.list.forEach(item => {
								item.address = item.leader_full_address.replace(/\,/g, '')
							})
							newArr = res.data.list;
						} else {
							this.$util.showToast({
								title: msg
							});
						}
						mescroll.endSuccess(newArr.length);
						//设置列表数据
						if (mescroll.num == 1) this.dataList = []; //如果是第一页需手动制空列表
						this.dataList = this.dataList.concat(newArr); //追加新数据
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					},
					fail: res => {
						mescroll.endErr();
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.page {
		.order-nav {
			width: 100vw;
			height: 90rpx;
			flex-direction: row;
			/* #ifdef H5 */
			// padding-top: 14rpx;
			/* #endif */
			/* #ifndef APP-PLUS */
			white-space: nowrap;
			/* #endif */
			background: #fff;
			display: flex;
			// border-bottom-left-radius: 24rpx;
			// border-bottom-right-radius: 24rpx;
			// padding-bottom: 30rpx;
			/* #ifdef H5 */
			// padding-bottom: 20rpx;
			/* #endif */
			position: fixed;
			left: 0;
			z-index: 998;

			.uni-tab-item {
				flex: 1;
				padding-left: 24rpx;
				padding-right: 24rpx;
			}

			.uni-tab-item-title {
				font-size: $font-size-base;
				display: block;
				height: 86rpx;
				line-height: 90rpx;
				border-bottom: 1px solid #fff;
				padding: 0 10rpx;
				flex-wrap: nowrap;
				/* #ifndef APP-PLUS */
				white-space: nowrap;
				/* #endif */
				text-align: center;
				font-size: 30rpx;
			}

			.uni-tab-item-title-active {
				display: block;
				height: 86rpx;
				border-bottom: 2px solid #ffffff;
				padding: 0 10rpx;
			}

			::-webkit-scrollbar {
				width: 0;
				height: 0;
				color: transparent;
			}
		}

		.order-group {
			.order-item {
				background-color: white;
				border-radius: $border-radius;
				margin: $margin-updown $margin-both;
				padding: $padding;


				.order-title {
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 2rpx solid $color-line;
					padding-bottom: $padding;

					text {
						color: $color-sub;
						font-size: $font-size-sub;
					}

					.color-red {
						color: #F63F3D;
					}
				}

				.msg-box {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 10rpx;

					.msg-spell {
						display: flex;
						align-items: center;
						font-size: $font-size-base;

						.msg-title {
							width: 200rpx;
							color: $color-title;
						}

						.msg-name {
							color: $color-sub;
						}
					}

					.msg-phone {
						display: flex;
						align-items: center;

						image {
							margin-right: 6rpx;
							width: 26rpx;
						}
					}
				}

				.msg-address {
					display: flex;
					align-items: flex-start;
					border-bottom: 2rpx solid $color-line;
					padding-bottom: $padding;
					padding-top: 10rpx;

					.address-title {
						font-size: $font-size-base;
						width: 200rpx;
					}

					.address-text {
						font-size: $font-size-sub;
						width: 480rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
						color: $color-sub;

					}
				}

				.footer-box {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding-top: $padding;

					text {
						font-size: 26rpx;
					}

					.btn-group {
						display: flex;

						button {
							background-color: white;
							width: 130rpx;
							height: 56rpx;
							line-height: 56rpx;
							margin: 0;
							color: #FF4544;
							font-size: 24rpx;
							padding: 0;
							border: 2rpx solid #FF4544;
							margin-left: $margin-both;
						}
					}
				}
			}

		}

		.none_data {
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;

			image {
				margin-top: 100rpx;
				width: 350rpx;
				height: 300rpx;
				margin-bottom: 120rpx;
			}

			text {
				font-size: $font-size-sub;
				color: $color-tip;
			}
		}
	}
</style>
<style scoped>

</style>
